package com.zhengtoon.mctoon.conv.controller;


import com.bjtoon.framework.entity.ResponseResult;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.dto.*;
import com.zhengtoon.mctoon.conv.dto.param.UploadInputParam;
import com.zhengtoon.mctoon.conv.dto.param.UploadOutputResult;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.SessionInfo;
import com.zhengtoon.mctoon.conv.utils.XssEncodeUtils;
import com.zhengtoon.mctoon.conv.service.ConvAttachmentService;
import com.zhengtoon.mctoon.conv.service.ConvPhotoInfoService;
import com.zhengtoon.mctoon.conv.service.ConvRemindRecordService;
import com.zhengtoon.mctoon.conv.vo.ConvPhotoVo;
import com.zhengtoon.mctoon.conv.vo.FeedBackVo;
import com.zhengtoon.mctoon.conv.vo.PageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/convphoto")
@Api(value = "convphoto服务", description = "convphoto服务")
public class ConvPhotoInfoController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ConvPhotoInfoController.class);

    @Autowired
    private ConvPhotoInfoService convPhotoInfoService;

    @Autowired
    private ConvAttachmentService convAttachmentService;

    @Autowired
    private ConvRemindRecordService convRemindRecordService;


    /**
     * 上传附件
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "上传附件", httpMethod = "POST", notes = "上传附件")
    @RequestMapping(value = "/uploadAttachment", method = RequestMethod.POST)
    public ResponseResult<List<UploadOutputResult>>
    uploadAttachment(HttpServletRequest request,
                     @ApiParam(required = true, name = "uploadInputVo", value = "云上传参数") @RequestBody @Valid List<UploadInputParam> uploadInputVo) {
        try {

            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }

            // 2.上传附件
            List<UploadOutputResult> voList = convAttachmentService.uploadAttachment(uploadInputVo);

            return ResponseResult.success(voList);
        } catch (BusinessException e) {
            return ResponseResult.fail(AppConstant.ERROR_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("uploadAttachment()方法error", e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "uploadAttachment()方法error");
        }
    }

    /**
     * 内容发布接口
     *
     * @param releaseDto 提交随手拍记录对象
     * @return ResponseResult
     */
    @ApiOperation(value = "内容发布接口 ", httpMethod = "POST", notes = "内容发布接口")
    @RequestMapping(value = "/addPhotos", method = RequestMethod.POST)
    public ResponseResult addPhotos(HttpServletRequest request,
                                    @ApiParam(required = true, name = "RequestReleaseDto", value = "提交随手拍记录对象") @RequestBody @Valid RequestReleaseDto releaseDto) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request); // 1. 获取session

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }

            if (StringUtils.isAnyBlank(releaseDto.getContent(), releaseDto.getLocation())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            releaseDto.setContent(XssEncodeUtils.xssEncode(releaseDto.getContent()));
            int rs = convPhotoInfoService.addPhotos(releaseDto, sessionInfo);
            if (rs == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("随手拍发布成功");

        } catch (BusinessException ex) {
            log.error("随手拍记录数据提交错误：" + ex.toString());
            return ResponseResult.fail(AppConstant.ERROR_CODE, ex.getMessage());
        } catch (Exception e) {
            log.error("随手拍记录数据提交方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "随手拍记录数据提交方法error");
        }
    }


    /**
     * 修改内容发布接口
     *
     * @param releaseDto 修改随手拍记录对象
     * @return ResponseResult
     */
    @ApiOperation(value = "修改内容发布接口 ", httpMethod = "POST", notes = "内容发布接口")
    @RequestMapping(value = "/updatePhotos", method = RequestMethod.POST)
    public ResponseResult updatePhotos(HttpServletRequest request,
                                       @ApiParam(required = true, name = "RequestReleaseDto", value = "修改随手拍记录对象") @RequestBody @Valid RequestReleaseDto releaseDto) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request); // 1. 获取session

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }

            if (StringUtils.isAnyBlank(releaseDto.getId(), releaseDto.getContent(), releaseDto.getLocation())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            releaseDto.setContent(XssEncodeUtils.xssEncode(releaseDto.getContent()));
            int rs = convPhotoInfoService.updatePhotos(releaseDto, sessionInfo);
            if (rs == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            if (rs == -1) {
                return ResponseResult.success("您已提交成功请等待审核员审核");
            }
            if (rs == -2) {
                return ResponseResult.success("您的随手拍已审核通过");
            }
            return ResponseResult.success("随手拍发布成功");

        } catch (BusinessException ex) {
            log.error("随手拍记录数据提交错误：" + ex.toString());
            return ResponseResult.fail(AppConstant.ERROR_CODE, ex.getMessage());
        } catch (Exception e) {
            log.error("随手拍记录数据提交方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "随手拍记录数据提交方法error");
        }
    }

    /**
     * 我的随手拍列表
     *
     * @param pageVo 分页参数
     * @return ResponseResult
     */
    @ApiOperation(value = "我的随手拍列表 ", httpMethod = "POST", notes = "我的随手拍列表")
    @RequestMapping(value = "/queryPhotoList", method = RequestMethod.POST)
    public ResponseResult queryPhotoList(HttpServletRequest request,
                                         @ApiParam(required = true, name = "pageVo", value = "分页参数") @RequestBody @Valid PageVo pageVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(pageVo.getPageNum(), pageVo.getPageSize())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            List<ResultConvPhotoDto> rs = convPhotoInfoService.queryPhotoList(pageVo.getPageNum(), pageVo.getPageSize(), sessionInfo.getUserId());
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取我的随手拍列表方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取我的随手拍列表方法error");
        }
    }

    /**
     * 随手拍详情
     *
     * @param convPhotoVo 随手拍记录id
     * @return ResponseResult
     */
    @ApiOperation(value = "随手拍详情 ", httpMethod = "POST", notes = "随手拍详情")
    @RequestMapping(value = "/queryPhotoDetailById", method = RequestMethod.POST)
    public ResponseResult queryPhotoDetailById(HttpServletRequest request,
                                               @ApiParam(required = true, name = "convPhotoVo", value = "随手拍记录id") @RequestBody @Valid ConvPhotoVo convPhotoVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isBlank(convPhotoVo.getId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            ResultConvPhotoInfoDto rs = convPhotoInfoService.queryPhotoDetailById(convPhotoVo.getId(), sessionInfo.getUserId());
            if (rs == null) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            log.debug("随手拍详情返回结果为：" + rs.toString());
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取我的随手拍详情方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取我的随手拍详情方法error()");
        }
    }

    /**
     * 随手拍处理反馈
     *
     * @param feedBackVo 用户反馈对象
     * @return ResponseResult
     */
    @ApiOperation(value = "随手拍处理反馈 ", httpMethod = "POST", notes = "随手拍处理反馈")
    @RequestMapping(value = "/userFeedBack", method = RequestMethod.POST)
    public ResponseResult userFeedBack(HttpServletRequest request,
                                       @ApiParam(required = true, name = "feedBackVo", value = "用户反馈对象") @RequestBody @Valid FeedBackVo feedBackVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(feedBackVo.getId(), String.valueOf(feedBackVo.getUserFeedBack()))) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            if (feedBackVo.getUserFeedBack() == 1) {
                if (StringUtils.isBlank(feedBackVo.getMessge())) {
                    return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
                }
            }
            feedBackVo.setMessge(XssEncodeUtils.xssEncode(feedBackVo.getMessge()));
            int rs = convPhotoInfoService.updateUserFeedBack(feedBackVo.getId(), feedBackVo.getUserFeedBack(), feedBackVo.getMessge());
            if (rs == 0) {
                return new ResponseResult(ConvPhotoExceptionCode.RESULT_IS_NULL);
            }
            return ResponseResult.success("反馈成功");
        } catch (Exception e) {
            log.error("随手拍处理反馈方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "随手拍处理反馈方法error");
        }
    }


    /**
     * 获取问题处理列表
     *
     * @param pageVo 分页参数
     * @return ResponseResult
     */
    @ApiOperation(value = "获取问题处理列表 ", httpMethod = "POST", notes = "获取问题处理列表")
    @RequestMapping(value = "/getProblemHandList", method = RequestMethod.POST)
    public ResponseResult getProblemHandList(HttpServletRequest request,
                                             @ApiParam(required = true, name = "pageVo", value = "分页参数") @RequestBody @Valid PageVo pageVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(pageVo.getPageNum(), pageVo.getPageSize())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }
            List<ResultProblemHandDto> rs = convPhotoInfoService.queryProblemHandList(pageVo.getPageNum(), pageVo.getPageSize());
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取问题处理列表方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取问题处理列表方法error");
        }
    }

    /**
     * 获取社会警示列表
     *
     * @param pageVo 分页参数
     * @return ResponseResult
     */
    @ApiOperation(value = "获取社会警示列表 ", httpMethod = "POST", notes = "获取社会警示列表")
    @RequestMapping(value = "/getSocialWarningList", method = RequestMethod.POST)
    public ResponseResult getSocialWarningList(HttpServletRequest request,
                                               @ApiParam(required = true, name = "pageVo", value = "分页参数") @RequestBody @Valid PageVo pageVo) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }
            if (StringUtils.isAnyBlank(pageVo.getPageSize(), pageVo.getPageNum())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_FAIL);
            }

            List<ResultPraisConvPhotoDto> rs = convPhotoInfoService.queryPraisPhotoList(pageVo.getPageNum(), pageVo.getPageSize(), sessionInfo.getUserId(), 3);
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取大赞北京列表的方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取大赞北京列表的方法error");
        }
    }

    /**
     * 获取有新评论的状态
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "获取有新评论的状态 ", httpMethod = "POST", notes = "获取有新评论的状态")
    @RequestMapping(value = "/getCommentStatus", method = RequestMethod.POST)
    public ResponseResult getCommentStatus(HttpServletRequest request) {
        try {
            // 1. 获取session
            SessionInfo sessionInfo = getSessionInfo(request);

            if (null == sessionInfo || StringUtils.isEmpty(sessionInfo.getUserId())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.SESSION_IS_FAIL);
            }

            Boolean rs = convRemindRecordService.queryCommentStatus(sessionInfo.getUserId());
            return ResponseResult.success(rs);
        } catch (Exception e) {
            log.error("获取有新评论的状态的方法error：" + e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "获取有新评论的状态的方法error");
        }
    }
}